<?php

namespace yak\layui\helper;

use Kordar\Tree\GenerateTreeByArray;
use Kordar\Tree\MenuIterator;
use yii\helpers\ArrayHelper;

/**
 * Class YakHelper
 * @package yak\layui\helper
 */
class YakHelper
{
    public static function config($field, $default = [])
    {
        return ArrayHelper::getValue(\Yii::$app->params, $field, $default);
    }

    public static function generateTreeByData($data)
    {
        $tree = (new GenerateTreeByArray())->treeAll($data);
        $sideBarTree = new \RecursiveIteratorIterator(new MenuIterator($tree),
            \RecursiveIteratorIterator::SELF_FIRST);

        $list = [];
        foreach ($sideBarTree as $item) {
            $prefix = str_repeat('　', $sideBarTree->getDepth()) . '┗';
            $list[$item['id']] = $prefix . ' ' . $item['title'];
        }

        return $list;
    }
}